/**
 * @ClassName BinarySearch
 * @Description 给定一个有序整型数组, 实现二分查找
 * @Author 李明忠
 * @Date 2025/5/20 20:53
 * @Version 1.0
 */
public class BinarySearch {
    public static int binarySearch(int[] arr, int num) {
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;
        while (left < right) {
            mid = left + right;
            if (arr[mid] == num) {
                return mid;
            } else if (arr[mid] < num) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        //二分查找条件：有序序列
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int temp = binarySearch(arr, 6);
        System.out.println(temp > 0 ? temp : "未找到！！");
    }
}
